package com.takeoff.iot.modbus.server.message.sender;

import com.takeoff.iot.modbus.common.message.ModbusMessage;
import com.takeoff.iot.modbus.common.message.factory.ModbusMessageFactory;
import com.takeoff.iot.modbus.common.utils.CacheUtils;
import com.takeoff.iot.modbus.common.utils.JudgeEmptyUtils;
import com.takeoff.iot.modbus.netty.channel.ModbusChannel;
import org.bouncycastle.util.encoders.Hex;


import lombok.extern.slf4j.Slf4j;

/**
 * 类功能说明：指令下发<br/>
 * 作者：zhengbzh <br/>
 */
@Slf4j
public class ModbusServerMessageSender implements ServerMessageSender{

//	private static final ModbusBytesFactory<Integer> BYTESFACTORY_SLOT = new ModbusSlotBytesFactory();




	public ModbusServerMessageSender(){

	}

	private <E> void sendMessage(ModbusMessageFactory<E> factory, String deviceIp, E... datas){
		if(JudgeEmptyUtils.isEmpty(CacheUtils.get(deviceIp))){
			log.info("未找到对应的客户端："+ deviceIp +" 的通讯连接，下发指令失败");
		}else{
			ModbusChannel channel = (ModbusChannel) CacheUtils.get(deviceIp);
			ModbusMessage message = factory.buildMessage(deviceIp, datas);
			log.info("待下发指令数据："+Hex.toHexString(message.toBytes()));
			channel.send(message);
		}
	}



}
